package com.xss.ipctest.provider;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.xss.ipctest.R;
import com.xss.ipctest.aidl.Book;
import com.xss.ipctest.model.User;

public class ProviderActivity extends AppCompatActivity {

    private static final String TAG = "ProviderActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_provider);
//        Uri uri=Uri.parse("content://com.xss.ipctest.book.provider");
//        getContentResolver().query(uri,null,null,null,null);
//        getContentResolver().query(uri,null,null,null,null);
//        getContentResolver().query(uri,null,null,null,null);
        Uri bookUri = Uri.parse("content://com.xss.ipctest.book.provider/book");
        ContentValues values = new ContentValues();
        values.put("_id", 6);
        values.put("name", "程序设计的艺术");
        getContentResolver().insert(bookUri, values);
        Cursor bookCursor = getContentResolver().query(bookUri, new String[]{"_id", "name"}, null, null, null);
        while (bookCursor.moveToNext()) {
            Book book = new Book();
            book.setBookId(bookCursor.getInt(0)) ;
            book.setBookName(bookCursor.getString(1));
            Log.i(TAG, "query book:" + book.toString());
        }
        bookCursor.close();

        Uri userUri = Uri.parse("content://com.xss.ipctest.book.provider/user");
        Cursor userCursor = getContentResolver().query(userUri, new String[]{"_id", "name", "sex"}, null, null, null);
        while (userCursor.moveToNext()) {
            User user = new User();
            user.setUserId(userCursor.getInt(0)) ;
            user.setUserName(userCursor.getString(1)) ;
            user.setMale(userCursor.getInt(2) == 1);
            Log.i(TAG, "query user:" + user.toString());
        }
        userCursor.close();
    }
}